package com.qdxwx.manage.controller.picture;

import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 图片Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/picture")
public class PictureViewController extends BaseController {
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private MemberTypeService memberTypeService;

    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) {
            return error404;
        }
        Picture picture = pictureService.get(id);
        if (picture == null) {
            return error404;
        }
        String member = "普通";
        if (picture.getLevelId() > 0) {
            MemberType memberType = memberTypeService.get(picture.getLevelId());
            if (memberType != null) {
                member = memberType.getTitle();
            }
        }
        if (picture.getSubjectId() > 0) {
            Subject subject = subjectService.get(picture.getSubjectId());
            if (subject != null) {
                mmap.put("subjectName", subject.getSubjectName());
            }
        }
        mmap.put("member", member);
        mmap.put("picture", picture);
        mmap.put("channel", channelService.get(picture.getChannelId()));
        return "picture/picture-view";
    }
}
